<?php
//
namespace app\circle\controller\admin;

use app\circle\model\CompanyModel;
use app\circle\model\MeetingCompanyModel;
use app\circle\model\MeetingModel;
use function PHPSTORM_META\map;
use think\Request;

class MeetingCompanyController extends BaseController
{

    private $companyModel;
    private $meetingModel;
    private $meetingCompanyModel;

    public function __construct(Request $request)
    {
        parent::__construct($request);
        $this->meetingModel = new MeetingModel();
        $this->companyModel  = new CompanyModel();
        $this->meetingCompanyModel  = new MeetingCompanyModel();
    }

    /**
     * 首页
     * @return mixed
     */
    public function index()
    {
        $map        = [];
        if($this->adminInfo['admin_power']==2){
            $map['dept_id'] = $this->adminInfo['dept_id'];
        }
        if($this->adminInfo['admin_power']==3){
            $map['admin_id'] = $this->adminInfo['admin_id'];
        }
        $query      = $this->request->request();
        $meetingName  = $this->request->param('meeting_name');
        if (!empty($meetingName)) {
            $map['meeting_name'] = ['like', '%' . $meetingName . '%'];
        }
        $list = $this->meetingModel->where($map)->paginate(12, false, ['query' => $query]);
        $this->assign('list', $list);
        return $this->fetch();
    }

    /**
     * 添加公司
     * @return mixed
     */
    public function add(){
        if($this->request->isAjax()){
            $data   = $this->request->param();
            $meetingInfo = $this->meetingModel->where('meeting_id',$data['meeting_id'])->find();
            $companyInfo = $this->companyModel->where('company_id',$data['company_id'])->find();
            $data['meeting_name'] = $meetingInfo['meeting_name'];
            $data['dept_id'] = $meetingInfo['dept_id'];
            $data['dept_name'] = $meetingInfo['dept_name'];
            $data['company_name'] = $companyInfo['company_name'];
            $data['company_logo'] = $companyInfo['company_logo'];
            $data['company_address'] = $companyInfo['company_address'];
            $data['company_code'] = build_unique_no('circle_meeting_company','company_code',6);
            $this->meetingCompanyModel->addData($data);
            $this->success('操作成功！','index');
        }
        $meetingId = $this->request->param('meeting_id');
        $this->assign('meetingId',$meetingId);
        $map=[];
        if ($this->adminInfo['admin_power']==2){
            $map['dept_id']=$this->adminInfo['dept_id'];
        }
        if ($this->adminInfo['admin_power']==3){
            $map['admin_id']=$this->adminInfo['admin_id'];
        }
        $meetingList = $this->meetingModel->where($map)->select()->toArray();
        $this->assign('meetingList',$meetingList);
        return $this->fetch();
    }
    /*
     * 获取公司列表
     */
    public function comList(){
        $companyName = $this->request->param('company_name');
        $map=[
            'is_examine'=>1
        ];
        if(!empty($companyName)){
            $map['company_name'] = ['like',"%".$companyName."%"];
        }
        $meetingId = $this->request->param('meeting_id',0);
        $deptId = $this->meetingModel->where('meeting_id',$meetingId)->value('dept_id');
        $recomList = $this->meetingCompanyModel->where('meeting_id',$meetingId)->column('company_id');
        $comList = $this->companyModel->where($map)->where('dept_id',$deptId)->where(['company_id'=>['notin',$recomList]])->select()->toArray();
        return $comList;
    }

    /**
     * 编辑
     */
    public function edit(){
        $meetingId = $this->request->param('meeting_id/d');
        $meetingName = $this->request->param('meeting_name');
        $this->assign('meetingName',$meetingName);
        $this->assign('meetingId',$meetingId);
        $comList = $this->meetingCompanyModel->where('meeting_id',$meetingId)->select();
        $this->assign('comList',$comList);
        return $this->fetch();
    }

    /**
     * 删除
     */
    public function del(){
        $meetingId = $this->request->param('meeting_id/d');
        $companyId = $this->request->param('company_id/d');
        $this->meetingCompanyModel->where(['meeting_id'=>$meetingId,'company_id'=>$companyId])->delete();
        $this->success('删除成功！');
    }

}
